<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ page import="java.util.List"%>
<%@ page
	import="com.google.appengine.api.datastore.DatastoreServiceFactory"%>
<%@ page import="com.google.appengine.api.datastore.DatastoreService"%>
<%@ page import="com.google.appengine.api.datastore.Query"%>
<%@ page import="com.google.appengine.api.datastore.Entity"%>
<%@ page import="com.google.appengine.api.datastore.FetchOptions"%>
<%@ page import="com.google.appengine.api.datastore.Key"%>
<%@ page import="com.google.appengine.api.datastore.KeyFactory"%>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>GuestBook</title>
<link type="text/css" rel="stylesheet" href="/stylesheets/main.css" />
</head>
<body>
	<%
		String anchestorKeyName = request.getParameter("numeCarteOaspeti");

		if (anchestorKeyName == null) {
			anchestorKeyName = "default";
		}
		pageContext.setAttribute("numeCarteOaspeti", anchestorKeyName);

		DatastoreService datastore = DatastoreServiceFactory
				.getDatastoreService();
		
		Key guestbookKey = KeyFactory.createKey("Carte", anchestorKeyName);
	    
		// Run an ancestor query to ensure we see the most up-to-date
		// view of the entries belonging to the selected anchestorName "Carte".
		Query query = new Query("Oaspete", guestbookKey).addSort("date",
				Query.SortDirection.DESCENDING);	
		List<Entity> oaspeti = datastore.prepare(query).asList(
				FetchOptions.Builder.withLimit(5));
		
		if (oaspeti.isEmpty()) {
	        %>
	<p>Guestbook '${fn:escapeXml(numeCarteOaspeti)}' has no messages.</p>
	<%
	    } else {
	        %>
	<p>Messages in Guestbook '${fn:escapeXml(numeCarteOaspeti)}'.</p>
	<%
	        for (Entity greeting : oaspeti) {
	            pageContext.setAttribute("content",
	                                     greeting.getProperty("continut"));
	            pageContext.setAttribute("date_content",
                        greeting.getProperty("date"));
	            %>
	<blockquote>${fn:escapeXml(content)}</blockquote>
	<blockquote>${fn:escapeXml(date_content)}</blockquote>
	<%
	        }
	    }
	%>
	<form action="/servletexample" method="post">
		<div>
			<textarea name="oaspete" rows="3" cols="60"></textarea>
		</div>
		<div>
			<input type="submit" value="Submit" />
		</div>
		<input type="hidden" name="numeCarteOaspeti"
			value="${fn:escapeXml(numeCarteOaspeti)}" />
	</form>
	<p>
		Lista are:
		<% out.println(oaspeti.size()); %>
		elemente!
	</p>
	<div>
		<ol>
			<%		
		for (Entity en : oaspeti){
			pageContext.setAttribute("content",
                    en.getProperty("continut"));
pageContext.setAttribute("date_content",
   en.getProperty("date"));
			%>
			<li>${fn:escapeXml(content)} : at(
				${fn:escapeXml(date_content)})</li>
			<% }; %>
		</ol>
	</div>
</body>
</html>